"""
@File    :   models.py
@Time    :   2024/02/16 23:36:58
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   菜单相关数据模型
"""

from datetime import datetime

from sqlalchemy import (
    BigInteger,
    Boolean,
    Column,
    DateTime,
    Integer,
    SmallInteger,
    String,
    UniqueConstraint,
)

from src.business.admin.system.menu.enums import MenuTypeEnum
from src.models.models import OrmBase


class Menu(OrmBase):
    """菜单数据模型"""

    __tablename__ = "system_menu"
    # 设置联合唯一(字段为None时不会触发, 例如: mysql是允许parent_id为None时, 存在多个相同的name的)
    __table_args__ = (UniqueConstraint("name", "parent_id"),)

    id = Column(BigInteger(), primary_key=True, autoincrement=True, comment="主键")
    name = Column(String(50), nullable=False, index=True, comment="菜单名称")
    permission = Column(
        String(100), default="", nullable=False, index=True, comment="权限标识"
    )
    type = Column(
        SmallInteger(),
        nullable=False,
        index=True,
        default=MenuTypeEnum.directory.value,
        comment="菜单类型",
    )  # 1目录, 2菜单, 3按钮
    path = Column(
        String(200), default="", nullable=False, comment="路由地址"
    )  # 外部地址以http(s)://开头
    icon = Column(String(100), default="", nullable=False, comment="菜单图标")
    component = Column(String(255), default="", nullable=False, comment="组件路径")
    component_name = Column(
        String(255), default="", nullable=False, comment="组件名称"
    )  # 对应静态路由的name参数
    visible = Column(
        Boolean(), default=True, nullable=False, comment="显示状态"
    )  # True显示 False隐藏 (选择隐藏时，路由将不会出现在侧边栏，但仍然可以访问)
    always_show = Column(
        Boolean(), default=True, nullable=False, comment="总是显示"
    )  # True是 False否 (选择不是时，当该菜单只有一个子菜单时，不展示自己，直接展示子菜单)
    keep_alive = Column(
        Boolean(), default=True, nullable=False, comment="缓存状态"
    )  # True缓存 False不缓存 (选择缓存时，则会被 `keep-alive` 缓存，必须填写「组件名称」字段)
    # 上级菜单ID(删除保护)
    parent_id = Column(
        Integer(), default=0, nullable=False, index=True, comment="上级菜单ID"
    )
    status = Column(
        Boolean(), default=True, nullable=False, comment="状态"
    )  # (True正常 False停用)
    remark = Column(String(300), default="", nullable=False, comment="备注")
    show_order = Column(
        Integer(), nullable=False, index=True, default=1, comment="显示顺序"
    )
    is_system = Column(
        Boolean(), default=False, nullable=False, comment="是否系统级别菜单"
    )  # 系统级别菜单不可修改与删除
    create_datetime = Column(
        DateTime(), nullable=False, default=datetime.now, index=True, comment="创建时间"
    )
    update_datetime = Column(
        DateTime(),
        nullable=False,
        default=datetime.now,
        onupdate=datetime.now,
        index=True,
        comment="修改时间",
    )
    # 创建者用户名(用户被删除时, 该值不改变)
    creator_username = Column(
        String(20), default="", nullable=False, comment="创建者用户名"
    )
    # 修改者用户名(用户被删除时, 该值不改变)
    updater_username = Column(
        String(20), default="", nullable=False, comment="修改者用户名"
    )
